﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
using System.Web.Security;
using TSV.Areas.Administration.Models;
using TSV.Models;
using TSV.Library.Common;
using System.Globalization;
using System.Collections;
using System.Configuration;

namespace TSV
{
    // Note: For instructions on enabling IIS6 or IIS7 classic mode, 
    // visit http://go.microsoft.com/?LinkId=9394801

    public class MvcApplication : System.Web.HttpApplication
    {
        private static List<string> chars = null;
        private string cookie = Constants.SERVER_COOKIE;
        protected void Application_BeginRequest(Object sender, EventArgs e)
        {
            CultureInfo ci = new CultureInfo("en-US");

            ci.DateTimeFormat.SetAllDateTimePatterns(

                new string[] { "dd/MM/yyyy" },

                'd'

            );

            System.Threading.Thread.CurrentThread.CurrentCulture = ci;

            System.Threading.Thread.CurrentThread.CurrentUICulture = ci;

        }

        public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

            routes.MapRoute(
                "Administration", // Route name
                "Administration", // URL with parameters
                new { controller = "AdminHome", action = "Index", id = UrlParameter.Optional } // Parameter defaults
            );

            //for frontend
            routes.MapRoute(
                "Article",
                "baiviet/{alias}.{html}",
                new { controller = "ArticleView", action = "Index", alias = "" }
            );
            routes.MapRoute(
                "GlobalSearch",
                "timkiem.html",
                new { controller = "Home", action = "GlobalSearch"}
            );
            routes.MapRoute(
                "Category",
                "danhmuc/{id}/{name}",
                new { controller = "Home", action = "SelectPartnerListByCatId", id = "" },
                new { id = @"\d+" }
            );
            routes.MapRoute(
                "RegisterEmail",
                "dangky/{email}",
                new { controller = "Home", action = "CreateEmail", email = "" }
            );
            routes.MapRoute(
                "PromotionInfo",
                "khuyenmai/{id}/{name}",
                new { controller = "CategoryView", action = "PromotionInfo", id = "" },
                new { id = @"\d+" }
            );
            routes.MapRoute(
                "Captcha",
                "Mabaove",
                new { controller = "Register", action = "SetCaptcha" }
            );
            routes.MapRoute(
               "AdvertisingView",
               "quangcao/{id}/{name}",
               new { controller = "AdvertisingView", action = "Detail",id="" },
               new { id = @"\d+" }
           );
            routes.MapRoute(
                "CategoryFilter",
                "danhmuc/timkiem",
                new { controller = "CategoryView", action = "SetFilter" }
            );
            routes.MapRoute(
                "PartnerAddress",
                "cuahang/{id}/{name}",
                new { controller = "CategoryView", action = "Detail", id = "" },
                new { id = @"\d+" }
            );
            routes.MapRoute(
                "NewsList",
                "tintuc.html",
                new { controller = "NewsViews", action = "Index" }                
            );            
            routes.MapRoute(
                "News",
                "tintuc/{id}/{title}",
                new { controller = "NewsViews", action = "Detail", id = "", title = UrlParameter.Optional },
                new { id = @"\d+" }
            );
            routes.MapRoute(
                "RegisterCollaborator",
                "cong-tac-vien.{html}",
                new { controller = "Register", action = "Collaborator" }
            );
            routes.MapRoute(
                "RegisterCard",
                "dang-ky-so-huu-the.{html}",
                new { controller = "Register", action = "Card" }
            );
            routes.MapRoute(
               "RegisterSuccess",
               "thong-bao.{html}",
               new { controller = "Register", action = "RegisterSuccess" }
           );
            routes.MapRoute(
                "RegisterMember",
                "doanh-nghiep-dang-ky.{html}",
                new { controller = "Register", action = "Member" }
            );
            routes.MapRoute(
                "default", // route name
                "{controller}/{action}/{id}", // url with parameters
                new { controller = "Home", action = "Index", id = UrlParameter.Optional } // parameter defaults
            );
        }


        /// <summary>
        /// Check content end with special characters that is defined in configuration
        /// </summary>
        /// <param name="content"></param>
        /// <returns></returns>
        private bool IsEndWithSpecialChars(string content)
        {
            if (chars == null)
            {
                chars = new List<string>();
                string[] splits = ConfigurationManager.AppSettings["IGNORE_EXTENSION"].Split(',');
                for (int i = 0; i < splits.Length; i++)
                {
                    chars.Add(splits[i]);
                }
            }

            bool result = false;
            for (int i = 0; i < chars.Count; i++)
            {
                if (content.EndsWith(chars[i]))
                {
                    result = true;
                    break;
                }
            }

            return result;
        }

        protected void Application_AuthenticateRequest(object sender, EventArgs e)
        {
            //string url = Request.Url.AbsolutePath;

            //HttpCookie httpCookie = Context.Request.Cookies[FormsAuthentication.FormsCookieName];
            //if (httpCookie == null) return;

            //FormsAuthenticationTicket ticket = FormsAuthentication.Decrypt(httpCookie.Value);
            //if (ticket == null || ticket.Expired) { return; }

            //FormsIdentity identity = new FormsIdentity(ticket);

            //UserData udata = UserData.CreateUserData(ticket.UserData);
            //AuthenticationProjectPrincipal principal = new AuthenticationProjectPrincipal(identity, udata);
            //Context.User = principal;

        }

        protected void Application_Start()
        {
            AreaRegistration.RegisterAllAreas();
            RegisterRoutes(RouteTable.Routes);

            // For Hit counter
            Application["Online"] = 0;
        }

        protected void Session_End(object sender, EventArgs e)
        {


        }
    }
}